package com.cmcc.aregister.ui.adapter;

import java.io.InputStream;
import com.cmcc.aregister.R;
import com.cmcc.aregister.ui.activity.UserGuideActivity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class UserGuideGalleryAdapter extends BaseAdapter {
	private static final String TAG = "UserGuideGalleryAdapter";
	private UserGuideActivity context;
	private int[] array = new int[] { R.drawable.userguide_img_1};
	private Resources res;
	BitmapFactory.Options options;

	public UserGuideGalleryAdapter(UserGuideActivity c) {
		context = c;
		res = context.getResources();
		options = new BitmapFactory.Options();
	}

	@Override
	public int getCount() {
		return array.length;
	}

	@Override
	public Object getItem(int position) {
		return array[position];
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		Log.i(TAG, " getView  positon=" + position);
		if (convertView == null) {
			viewHolder = new ViewHolder();
			View view = LayoutInflater.from(context).inflate(
					R.layout.item_userguide_gallery, null);
			viewHolder.img = (ImageView) view
					.findViewById(R.id.imageview_item_userguide_gallery);
			view.setTag(viewHolder);
			convertView = view;
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		Bitmap bitmap = obtainPictureBitmap(context, array[position]);
		viewHolder.img.setImageBitmap(bitmap);	
		viewHolder.img.setDrawingCacheEnabled(false);
		return convertView;
	}

	/**
	 * 读drawable包下图片
	 * 
	 * @return
	 */
	public static Bitmap obtainPictureBitmap(Context myContext, int position) {
		try {
			InputStream inputStream = myContext.getResources().openRawResource(
					position);
			BitmapFactory.Options opt = new BitmapFactory.Options();
			opt.inPreferredConfig = Bitmap.Config.RGB_565;
			opt.inPurgeable = true;
			opt.inInputShareable = true;
			Bitmap bitmap = BitmapFactory.decodeStream(inputStream, null, opt);
			return bitmap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	class ViewHolder {
		ImageView img;
	}
	
	



}
